View Javadoc
1   package org.apache.maven.surefire.junitcore.pc;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.surefire.report.ConsoleLogger;
23  import org.apache.maven.surefire.util.internal.DaemonThreadFactory;
24  
25  import java.util.concurrent.ExecutorService;
26  import java.util.concurrent.Executors;
27  import java.util.concurrent.ThreadFactory;
28  
29  /**
30   * The factory of {@link SchedulingStrategy}.
31   *
32   * @author Tibor Digana (tibor17)
33   * @since 2.16
34   */
35  public class SchedulingStrategies
36  {
37      private static final ThreadFactory DAEMON_THREAD_FACTORY = DaemonThreadFactory.newDaemonThreadFactory();
38  
39      /**
40       * @param logger current error logger
41       * @return sequentially executing strategy
42       */
43      public static SchedulingStrategy createInvokerStrategy( ConsoleLogger logger )
44      {
45          return new InvokerStrategy( logger );
46      }
47  
48      /**
49       * @param logger current error logger
50       * @param nThreads fixed pool capacity
51       * @return parallel scheduling strategy
52       */
53      public static SchedulingStrategy createParallelStrategy( ConsoleLogger logger, int nThreads )
54      {
55          return new NonSharedThreadPoolStrategy( logger,
56                                                  Executors.newFixedThreadPool( nThreads, DAEMON_THREAD_FACTORY ) );
57      }
58  
59      /**
60       * @param logger current error logger
61       * @return parallel scheduling strategy with unbounded capacity
62       */
63      public static SchedulingStrategy createParallelStrategyUnbounded( ConsoleLogger logger )
64      {
65          return new NonSharedThreadPoolStrategy( logger, Executors.newCachedThreadPool( DAEMON_THREAD_FACTORY ) );
66      }
67  
68      /**
69       * The <tt>threadPool</tt> passed to this strategy can be shared in other strategies.
70       * <p/>
71       * The call {@link SchedulingStrategy#finished()} is waiting until own tasks have finished.
72       * New tasks will not be scheduled by this call in this strategy. This strategy is not
73       * waiting for other strategies to finish. The {@link org.junit.runners.model.RunnerScheduler#finished()} may
74       * freely use {@link SchedulingStrategy#finished()}.
75       *
76       * @param logger current error logger
77       * @param threadPool thread pool possibly shared with other strategies
78       * @return parallel strategy with shared thread pool
79       * @throws NullPointerException if <tt>threadPool</tt> is null
80       */
81      public static SchedulingStrategy createParallelSharedStrategy( ConsoleLogger logger, ExecutorService threadPool )
82      {
83          if ( threadPool == null )
84          {
85              throw new NullPointerException( "null threadPool in #createParallelSharedStrategy" );
86          }
87          return new SharedThreadPoolStrategy( logger, threadPool );
88      }
89  }